home *** CD-ROM | disk | FTP | other *** search
/ Programming a Multiplayer FPS in DirectX / Programming a Multiplayer FPS in DirectX (Companion CD).iso / DirectX / dxsdk_oct2004.exe / dxsdk.exe / Utilities / MView / texture.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2004-09-30  |  4.0 KB  |  169 lines

  1. /*//////////////////////////////////////////////////////////////////////////////
  2. //
  3. // File: texture.cpp
  4. //
  5. // Copyright (C) 1999 Microsoft Corporation. All Rights Reserved.
  6. //
  7. //
  8. //////////////////////////////////////////////////////////////////////////////*/
  9.  
  10. #include "mviewpch.h"
  11.  
  12. HRESULT
  13. TrivialData::NewTexture(char *szFilename, LPDIRECT3DTEXTURE9 *ppTexture)
  14. {
  15.      HRESULT hr = S_OK;
  16.     STextureCache *ptcCur;
  17.     DWORD cchFilename;
  18.     DWORD cchPath;
  19.     char szPath[1024];
  20.     char *szName;
  21.  
  22.     // first search the loaded textures
  23.     ptcCur = m_ptcTextureCacheHead;
  24.     while (ptcCur != NULL)
  25.     {
  26.         // if found just return that texture
  27.         if (strcmp(ptcCur->szFilename, szFilename) == 0)
  28.         {
  29.             *ppTexture = (LPDIRECT3DTEXTURE9)ptcCur->pTexture;
  30.  
  31.             if (ptcCur->pTexture != NULL)
  32.                 ptcCur->pTexture->AddRef();
  33.  
  34.             goto e_Exit;
  35.         }
  36.  
  37.         ptcCur = ptcCur->pNext;
  38.     }
  39.  
  40.     // if not found, load the texture and add an entry to the cache
  41.     ptcCur = new STextureCache;
  42.     if (ptcCur == NULL)
  43.     {
  44.         hr = E_OUTOFMEMORY;
  45.         goto e_Exit;
  46.     }
  47.  
  48.     // copy the filename
  49.     cchFilename = strlen(szFilename) + 1;
  50.     ptcCur->szFilename = new char[cchFilename];
  51.     if (ptcCur->szFilename == NULL)
  52.     {
  53.         hr = E_OUTOFMEMORY;
  54.         goto e_Exit;
  55.     }
  56.     memcpy(ptcCur->szFilename, szFilename, cchFilename);
  57.  
  58.     // load the file, if it fails, just set to NULL
  59.     cchPath = GetFullPathName(szFilename, sizeof(szPath), szPath, &szName);
  60.     if ((cchPath == 0) || (sizeof(szPath) <= cchPath))
  61.         return E_FAIL;
  62.  
  63.     hr = D3DXCreateTextureFromFile(m_pDevice, szName, (LPDIRECT3DTEXTURE9*)&ptcCur->pTexture);
  64.     if (FAILED(hr))
  65.         ptcCur->pTexture = NULL;
  66.  
  67.     // if that failed try the current directory
  68.     if (FAILED(hr))
  69.     {
  70.         hr = D3DXCreateTextureFromFile(m_pDevice, szFilename, (LPDIRECT3DTEXTURE9*)&ptcCur->pTexture);
  71.         if (FAILED(hr))
  72.             ptcCur->pTexture = NULL;
  73.     }
  74.  
  75.     // be sure to keep an addref for the cache
  76.     *ppTexture = (LPDIRECT3DTEXTURE9)ptcCur->pTexture;
  77.  
  78.     if (ptcCur->pTexture != NULL)
  79.         ptcCur->pTexture->AddRef();
  80.  
  81.     // link into cache
  82.     ptcCur->pNext = m_ptcTextureCacheHead;
  83.     m_ptcTextureCacheHead = ptcCur;
  84.  
  85.  
  86.  
  87. e_Exit:
  88.     return hr;
  89. }
  90.  
  91.  
  92. HRESULT
  93. TrivialData::NewCubeTexture(char *szFilename, LPDIRECT3DCUBETEXTURE9 *ppTexture)
  94. {
  95.      HRESULT hr = S_OK;
  96.     STextureCache *ptcCur;
  97.     DWORD cchFilename;
  98.     DWORD cchPath;
  99.     char szPath[1024];
  100.     char *szName;
  101.  
  102.     // first search the loaded textures
  103.     ptcCur = m_ptcTextureCacheHead;
  104.     while (ptcCur != NULL)
  105.     {
  106.         // if found just return that texture
  107.         if (strcmp(ptcCur->szFilename, szFilename) == 0)
  108.         {
  109.             *ppTexture = (LPDIRECT3DCUBETEXTURE9)ptcCur->pTexture;
  110.             ptcCur->pTexture->AddRef();
  111.             goto e_Exit;
  112.         }
  113.  
  114.         ptcCur = ptcCur->pNext;
  115.     }
  116.  
  117.     // if not found, load the texture and add an entry to the cache
  118.     ptcCur = new STextureCache;
  119.     if (ptcCur == NULL)
  120.     {
  121.         hr = E_OUTOFMEMORY;
  122.         goto e_Exit;
  123.     }
  124.  
  125.     // copy the filename
  126.     cchFilename = strlen(szFilename) + 1;
  127.     ptcCur->szFilename = new char[cchFilename];
  128.     if (ptcCur->szFilename == NULL)
  129.     {
  130.         hr = E_OUTOFMEMORY;
  131.         goto e_Exit;
  132.     }
  133.     memcpy(ptcCur->szFilename, szFilename, cchFilename);
  134.  
  135.     // load the file, if it fails, just set to NULL
  136.     cchPath = GetFullPathName(szFilename, sizeof(szPath), szPath, &szName);
  137.     if ((cchPath == 0) || (sizeof(szPath) <= cchPath))
  138.         return E_FAIL;
  139.  
  140.     hr = D3DXCreateCubeTextureFromFile(m_pDevice, szName, (LPDIRECT3DCUBETEXTURE9*)&ptcCur->pTexture);
  141.     if (FAILED(hr))
  142.         ptcCur->pTexture = NULL;
  143.  
  144.     // if that failed try the current directory
  145.     if (FAILED(hr))
  146.     {
  147.         hr = D3DXCreateCubeTextureFromFile(m_pDevice, szFilename, (LPDIRECT3DCUBETEXTURE9*)&ptcCur->pTexture);
  148.         if (FAILED(hr))
  149.             ptcCur->pTexture = NULL;
  150.     }
  151.  
  152.     // be sure to keep an addref for the cache
  153.     *ppTexture = (LPDIRECT3DCUBETEXTURE9)ptcCur->pTexture;
  154.  
  155.     if (ptcCur->pTexture != NULL)
  156.         ptcCur->pTexture->AddRef();
  157.  
  158.     // link into cache
  159.     ptcCur->pNext = m_ptcTextureCacheHead;
  160.     m_ptcTextureCacheHead = ptcCur;
  161.  
  162.  
  163.  
  164. e_Exit:
  165.     return hr;
  166. }
  167.  
  168.  
  169.